<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典名称" prop="dictTypeName">
        <el-input v-model="queryParams.dictTypeName" placeholder="请输入字典名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictTypeCode">
        <el-input v-model="queryParams.dictTypeCode" placeholder="请输入字典类型" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <nu-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</nu-button>
        <nu-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</nu-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <nu-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:dict:add']">新增</nu-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @cell-mouse-enter="cellMouseClick" height="calc(100% - 160px)">
      <el-table-column label="字典编号" align="center" prop="dictNo" width="80px" />
      <el-table-column label="字典名称" align="center" prop="dictTypeName" :show-overflow-tooltip="true" />
      <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/' + scope.row.dictTypeCode" class="link-type">
            <span>{{ scope.row.dictTypeCode }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <nu-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</nu-button>
          <nu-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</nu-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictTypeName">
          <el-input v-model="form.dictTypeName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictTypeCode">
          <el-input v-model="form.dictTypeCode" placeholder="请输入字典类型" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <nu-button type="primary" @click="submitForm">确 定</nu-button>
        <nu-button @click="cancel">取 消</nu-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache,
} from "@/api/system/dict/type";
import nuButton from "@/components/base/button";

export default {
  name: "Dict",
  dicts: ["sys_normal_disable"],
  components: { nuButton },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictTypeName: '',
        dictTypeCode: ''
        // status: "0"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictTypeName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
        ],
        dictTypeCode: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
        ],
      },
      tableActiveRow: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      let postData = {
        ...this.queryParams,
      };
      this.$httpGet(window.api.getDictTypes, postData)
        .then((response) => {
          if (response.code == 200) {
            this.typeList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        })
        .catch((err) => { });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictNo: undefined,
        dictTypeName: undefined,
        dictTypeCode: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dictNo);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    cellMouseClick(row, column, cell, event) {
      this.tableActiveRow = row
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      row = this.tableActiveRow
      let postData = {
        dictTypeCode: row.dictTypeCode,
      };
      this.$httpGet(window.api.getDictType, postData)
        .then((response) => {
          if (response.code == 200) {
            this.form = response.data;
            this.open = true;
            this.title = "修改字典类型";
          }
        })
        .catch((err) => { });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$httpPost(window.api.saveDictType, this.form)
            .then((response) => {
              if (response.code == 200) {
                if (this.form.dictNo != undefined) {
                  this.$modal.msgSuccess("修改成功");
                } else {
                  this.$modal.msgSuccess("新增成功");
                }
                this.open = false;
                this.getList();
              }
            })
            .catch((err) => { });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      row = this.tableActiveRow
      const dictIds = row.dictNo;
      this.$modal
        .confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？')
        .then(() => {
          return this.$httpGet(window.api.deleteDictType, { dictNo: dictIds })

        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dict/type/export",
        {
          ...this.queryParams,
        },
        `type_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch("dict/cleanDict");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  height: 100%
}
</style>